import os
from time import gmtime, strftime

date = strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())

class PackageInfoBase(type):
    def __new__(cls, name, bases, userattrs):
        if name == 'AbstractPackageInfo':
            return super(PackageInfoBase, cls).__new__(cls, name, bases, userattrs)

        attrs = dict(item for item in DefaultPackageInfo.__dict__.items()
            if not item[0].startswith('_'))
        attrs.update(userattrs)

        if attrs['py_module_name'] is None:
            attrs['py_module_name'] = attrs['name']

        if attrs['deb_package_name'] is None:
            attrs['deb_package_name'] = attrs['name']

        if not 'changelog_core' in attrs:
            raise Exception, 'info.changelog_core required'

        if not 'version' in attrs:
            raise Exception, 'info.version required'

        return super(PackageInfoBase, cls).__new__(cls, name, bases, attrs)


class AbstractPackageInfo(object):
    __metaclass__ = PackageInfoBase

    @classmethod
    def get_replaces(cls):
        replaces = {
            '%NAME%': cls.name,
            '%VERSION%': cls.version,
            '%DEB_PACKAGE_NAME%': cls.deb_package_name,
            '%PY_MODULE_NAME%': cls.py_module_name,
            '%MAINTAINER%': cls.maintainer,
            '%MAINTAINER_EMAIL%': cls.maintainer_email,
            '%DESCRIPTION%': cls.description,
            '%DEB_DEPENDS%': ', '.join(cls.deb_depends),
            '%DEB_BUILD_DEPENDS%': ', '.join(cls.deb_build_depends),
            '%URL%': cls.url,
            '%LICENSE%': cls.license,
            '%DATE%': date,
            '%CLASSIFIERS_LIST%': repr(cls.classifiers),
            '%DESTDIR%': cls.install_dir,
        }
        replaces.update(cls.additional_replaces)
        return replaces

    class build:
        sources_dir = os.path.realpath('.')
        build_dir = sources_dir
        result_dir = sources_dir + '/dist'


class DefaultPackageInfo(object):
    deb_build_depends = [
        'debhelper (>= 5)',
        'tar',
    ]
    deb_depends = []
    classifiers = [
        'Operating System :: OS Independent',
        'Programming Language :: Python',
    ]
    install_dir = '/tmp'
    license = 'Unknown'

    py_module_name = None
    deb_package_name = None
    additional_replaces = {}

#    changelog = None
#    version = None




